<?php
// 本类由系统自动生成，仅供测试用途
class OrderAction extends Action{
    public function index(){
        $shoppingCar = M('shopping_car');
        $uid = session('uid');
        $where['userId'] = $uid;
        $list = $shoppingCar->where($where)->select();
        if(!$list){
            $this->assign('is_show','true');
        }else{
            $this->assign('is_show','false');
        }
        $goods = M('detail');
        foreach ($list as $key => $value) {
            $where['id'] = $list[$key]['goodsId'];
            $one = $goods->where($where)->find();
            $list[$key]['name'] = $one['name'];
            $list[$key]['n_price'] = $one['n_price'];
            if (strpos($one['color_img'], ',')) {
                $list[$key]['img'] = (String)explode(",", $one['color_img'])[0];
            }else {
                $list[$key]['img'] = $one['color_img'];
            }
        }
        $this->assign("content", $list);
        $detail = M('detail');
        $detailList = $detail->select();
        foreach ($detailList as $key => $value) {
            if (strpos($detailList[$key]['color_img'], ",")) {
                $detailList[$key]['color_img'] = (String)explode(",", $detailList[$key]['color_img'])[0];
            }
        }
        shuffle($detailList);
        $this->assign('list', $detailList);
//		print_r($detailList);die;
		
        $this->display('order');
		
    }
	public function sidecar(){
		$shoppingCar = M('shopping_car');
        $uid = session('uid');
        $where['userId'] = $uid;
        $list = $shoppingCar->where($where)->select();
        if(!$list){
            $this->assign('is_show','true');
        }else{
            $this->assign('is_show','false');
        }
        $goods = M('detail');
        foreach ($list as $key => $value) {
            $where['id'] = $list[$key]['goodsId'];
            $one = $goods->where($where)->find();
            $list[$key]['name'] = $one['name'];
            $list[$key]['n_price'] = $one['n_price'];
            if (strpos($one['color_img'], ',')) {
                $list[$key]['img'] = (String)explode(",", $one['color_img'])[0];
            }else {
                $list[$key]['img'] = $one['color_img'];
            }
        }
		$this->ajaxReturn($list);
	}

    public function getInfo(){
        //获取单个商品信息
        $where['id'] = I('id');
        $detail = M('detail');
        $oneGet = $detail->where($where)->find();

        //展示图
        if (strpos($oneGet['color_img'], ',')) {
            $oneGet['color_img'] = explode(',', $oneGet['color_img']);
        }

        //颜色
        if (strpos($oneGet['color'], ',')) {
            $oneGet['color'] = explode(',', $oneGet['color']);
        } else {
            $oneGet['color'] = (array)$oneGet['color'];
        }

        //尺码
        if (strpos($oneGet['size'], ';')) {
            $oneGet['size'] = explode(';', $oneGet['size']);
            foreach ($oneGet['size'] as $key => $value) {
                if (strpos($oneGet['size'][$key], ',')) {
                    $oneGet['size'][$key] = explode(',', $oneGet['size'][$key]);
                } else {
                    $oneGet['size'][$key] = (array)$oneGet['size'][$key];
                }
            }
        } else {
            if (strpos($oneGet['size'], ',')) {
                $oneGet['size'] = explode(',', $oneGet['size']);
            } else {
                $oneGet['size'] = (array)$oneGet['size'];
            }
        }
        if ($oneGet) {
            $this->ajaxReturn($oneGet, '', 1);
        } else {
            $this->ajaxReturn($oneGet, '', 0);
        }
        $this->display('order');
    }

    public function change(){
        $uid = session('uid');
        $data['goodsId'] = I('goodsId');
        $data['userId'] = $uid;
        $data['size'] = I('size');
        $data['goodsNum'] = I('goodsNum');
        $data['color'] = I('color');
        $goodsOrder = M('shopping_car');
        $where['goodsId'] = I('goodsId');
        $where['userId'] = $uid;
        $one = $goodsOrder->where($where)->save($data);
        if ($one) {
            $this->ajaxReturn('', '', 1);
        } else {
            $this->ajaxReturn('', '', 0);
        }
    }
    //删除商品
    public function delete(){
        $uid = session('uid');
        $where['goodsId'] = I('goodsId');
        $where["userId"] = $uid;
        $where['size'] = I('size');
        $where['color'] = I('color');
        $goodsOrder = M('shopping_car');
        $delete = $goodsOrder->where($where)->delete();
        if ($delete) {
            $this->ajaxReturn('', '', 1);
        } else {
            $this->ajaxReturn('', '', 0);
        }
    }
    //判断购物车是否为空
    public function is_empty(){
        $shoppingCar = M('shopping_car');
        $uid = session('uid');
        $where['userId'] = $uid;
        $list = $shoppingCar->where($where)->select();
        if(!$list){
            $this->ajaxReturn('','',1);
        }else{
            $this->ajaxReturn('','',0);
        }
    }

    public function order_form(){
        $arr = $_POST['orderInfo'];
        session('orderInfo', $arr);
        $this->ajaxReturn('', '', 1);
    }
    //前台展示订单
    public function order_form_sub(){
        $arr = session('orderInfo');
        $detail = M('detail');
        foreach ($arr as $key => $value) {
            $whereId['id'] = $arr[$key]['goodsId'];
            $one = $detail->where($whereId)->find();
            if (strpos($one['color_img'], ';')) {
                $one['color_img'] = explode(';', $one['color_img'])[0];
                if (strpos($one['color_img'], ',')) {
                    $arr[$key]['img'] = (String)explode(',', $one['color_img'])[0];
                } else {
                    $arr[$key]['img'] = $one['color_img'];
                }
            }else{
                if (strpos($one['color_img'], ',')) {
                    $arr[$key]['img'] = (String)explode(',', $one['color_img'])[0];
                }else{
                    $arr[$key]['img'] = $one['color_img'];
                }
            }
            $arr[$key]['price'] = $one['n_price'];
            $arr[$key]['total'] = $one['n_price'] * $arr[$key]['num'];
            $total_all += $arr[$key]['total'];
            $total_num += $arr[$key]['num'];
            $arr[$key]['name'] = $one['name'];
        }
//        echo $total_all;
        session('total', $total_all);
        $this->assign('all_total', $total_all);
        $this->assign('total_num',$total_num);
        $this->assign('orders', $arr);
        $this->display('order_form');
    }

    //后台生成订单
    public function ordersSub(){
        $nowTime = time();
        $rand = rand(0, 100);
        $orderNumber = $nowTime . $rand;
        $arr = session('orderInfo');
        $uid = session('uid');
        $address = $_POST['address'];
        $tel = $_POST['tel'];
        $addressCode = $_POST['addressCode'];
        $goodsMaster = $_POST['goodsMaster'];
        $payWay = $_POST['payWay'];
        //实例化购物车表
        $shop_cart = M('shopping_car');
        //实例化订单表
        $orderList = M('order_list');
        $i = 0;
        foreach ($arr as $key => $value) {
            $data['userId'] = $uid;
            $data['goodsId'] = $arr[$key]['goodsId'];
            $data['size'] = $arr[$key]['size'];
            $data['color'] = $arr[$key]['color'];
            $data['num'] = $arr[$key]['num'];
            $data['price'] = $arr[$key]['price'];
            $data['address'] = $address;
            $data['tel'] = $tel;
            $data['addressCode'] = $addressCode;
            $data['goodsMaster'] = $goodsMaster;
            $data['payWay'] = $payWay;
            $data['orderNumber'] = $orderNumber;
            $data['total'] = session('total');
            $res = $orderList->add($data);
            if ($res) {
                $i++;
                $whereDelete['goodsId'] = $arr[$key]['goodsId'];
                $whereDelete['size'] = $arr[$key]['size'];
                $whereDelete['color'] = $arr[$key]['color'];
                $shop_cart->where($whereDelete)->delete();
            } else {
                break;
            }
        }
        session('ordersNumber', $orderNumber);
        if ($i == count($arr)) {
            $this->ajaxReturn("", "", 1);
        } else {
            $this->ajaxReturn('', '', 0);
        }
    }
    //展示订单
    public function orderShow(){
	    $ordersList = M('order_list');
        $ordersNum = session('ordersNumber');
        $total = session('total');
        $this->assign('total',$total);
        $this->assign('ordersNum',$ordersNum);

        $this->display('order_show');
    }

    public function addshxx(){
        $uid = session('uid');
        $data['sh_name'] = I('sh_name');
        $data['address1'] = I('address1');
        $data['address2'] = I('address2');
        $data['postcode'] = I('postcode');
        $data['phone_num'] = I('phone_num');
        $users = M("users");
        $id = $uid;
        $where['id'] =$id;
        $res = $users->where($where)->save($data);
        if ($res) {
            $this->ajaxReturn('上传成功');
        } else {
            $this->ajaxReturn('上传失败');
        }
    }

	public function get_addr(){
		$uid = session('uid');
		$users = M("users");
		$id = $uid;
		$where['id'] =$id;
		$res = $users->where($where)->find();
		$this->ajaxReturn($res);
	}
	
	
	public function sub_form(){
		$this->display();
	}
	
}